package com.sjzny.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.sjzny.bean.RespBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class CaptchaController extends BaseController{
    @GetMapping("/captcha")
    public RespBean captcha() {
        //创建验证码的对象
        LineCaptcha captcha = new LineCaptcha(120, 35, 4, 10);

        //获取验证码文本
        String captchaCode = captcha.getCode();

        //通过雪花算法生成唯一ID
        String captchaId = IdUtil.getSnowflakeNextIdStr();

        //将验证码唯一ID和验证码文本保存在Redis中
        redisUtil.set(captchaId,captchaCode,120);

        //将验证码转化成Base64文本
        String captchaImageBass64Data = captcha.getImageBase64Data();

        //创建Map保存验证码ID和Base64编码
        HashMap<String,Object> map = new HashMap<>();
        map.put("captchaId", captchaId);
        map.put("captchaImageBass64Data", captchaImageBass64Data);

        //以JSON形式返回Map
        return RespBean.ok("",map);
    }
}
